const { DataTypes, sequelize } = require("../config/sequelizeCommon");

const checkTableExistsQuery = `
SELECT table_name
  FROM information_schema.tables
  WHERE table_schema = 'OrderingSystem' AND table_name = 'customers'
`;

(() => {
    try {
        const existingTables = sequelize.query(checkTableExistsQuery, {
            type: sequelize.QueryTypes.SELECT,
        });

        const Customer =  sequelize.define("Customer", {
            id: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true,
            },
            //注册时间
            time: {
                type: DataTypes.DATE,
                defaultValue: DataTypes.NOW,
                allowNull: false,
            },
            //姓名
            name: {
                type: DataTypes.STRING(50),
                allowNull: false,
            },
            //头像
            head: {
                type: DataTypes.STRING(255),
                allowNull: false,
            },
            //性别
            sex: {
                type: DataTypes.STRING(20),
                allowNull: false,
            },
            //职业
            position: {
                type: DataTypes.STRING(50),
                allowNull: false,
            },
        });

        if (existingTables.length === 0) {
            // 表不存在，创建表
            Customer.sync();
        }

        // 导出 Customer
        module.exports = { Customer };
    } catch (error) {
        console.error("Error checking or creating user table:", error);
    }
})();